#include "public.h"
#include "Foc_control.h"
#include "bsp_uart.h"
#include "pid_controller.h"
static void on_uart_line_received(const char *line, void *ctx);


typedef struct {
    int id;
} my_uart_ctx_t;   // 串口id
static my_uart_ctx_t uart0_ctx = { .id = 0 };

void app_main(void) {
    Foc_Init(12.6,7,-1,10,0.01);   // 初始化
    
    uart_line_init(UART_NUM_0, 1, 3, 115200);
    uart_line_register_callback(UART_NUM_0, on_uart_line_received, &uart0_ctx);

    Foc_Run();    // 开始执行
}


static void on_uart_line_received(const char *line, void *ctx) {
    ESP_LOGI("APP", "收到串口数据: '%s'", line);

    // 去除结尾的换行符和回车符
    char cleaned_line[128];
    strncpy(cleaned_line, line, sizeof(cleaned_line));
    cleaned_line[sizeof(cleaned_line) - 1] = '\0'; // 安全终止
    char *newline = strpbrk(cleaned_line, "\r\n");
    if (newline) *newline = '\0';

    // 解析为 float
    foc_struct.motor_target = strtof(cleaned_line, NULL);
    ESP_LOGI("APP", "解析出的 motor_target = %.2f", foc_struct.motor_target);
}

